home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP14.TXT < prev    next >
Text File  |  1990-08-08  |  15KB  |  323 lines

  1.  
  2.                      Chapter 14 - Example Programs
  3.  
  4.  
  5.                              WHY THIS CHAPTER?
  6.  
  7.              Although  every  program  in this tutorial has  been  a
  8.         complete  program,  each  one  has also been  a  very  small
  9.         program intended to teach you some principle of  programming
  10.         in  C.   It  would do you a disservice to leave you at  that
  11.         point  without introducing you to a few larger  programs  to
  12.         illustrate  how  to  put together the  constructs  you  have
  13.         learned  to create a major program.   This chapter  contains
  14.         four  programs  of increasing complexity,  each designed  to
  15.         take  you  into a higher plateau of  programming,  and  each
  16.         designed to be useful to you in some way.
  17.  
  18.              DOSEX will illustrate how to make DOS system calls  and
  19.         will teach you,  through self-study, how the system responds
  20.         to  the  keyboard.   WHATNEXT  reads commands input  on  the
  21.         command line and will aid you in setting up a variable batch
  22.         file,  one  that requests an operator input and responds  to
  23.         the  input  by branching to a different part  of  the  batch
  24.         file.
  25.  
  26.              LIST  is  the source code for the program you  used  to
  27.         print  out the C source files when you began studying C with
  28.         the aid of this tutorial.  Finally we come to VC, the Visual
  29.         Calculator,  which  you should find to be a  useful  program
  30.         even  if you don't study its source code.   VC uses most  of
  31.         the  programming  techniques we have studied in this  course
  32.         and  a few that we never even mentioned such  as  separately
  33.         compiled subroutines.
  34.  
  35.              We  will  take a look at the example programs one at  a
  36.         time  but  without  a complete explanation of  any  of  them
  37.         because  you  have  been studying C for some  time  now  and
  38.         should be able to read and understand most of these programs
  39.         on  your  own.
  40.  
  41.                      DOSEX.C - The DOS Example Program
  42.  
  43.              The  copy of DOS that you received with your IBM-PC  or
  44.         compatible has about 80 internal DOS calls that you can  use
  45.         as  a programmer to control your peripheral devices and read
  46.         information  or status from them.   Some of the earlier  IBM
  47.         DOS manuals, DOS 2.0 and earlier, have these calls listed in
  48.         the back of the manual along with how to use them.   Most of
  49.         the  manuals  supplied  with compatible  computers  make  no
  50.         mention  of  these  calls even  though  they  are  extremely
  51.         useful.   These  calls  can  be  accessed  from  nearly  any
  52.         programming  language but they do require some initial study
  53.         to learn how to use them.   This program is intended to  aid
  54.         you in this study.
  55.  
  56.  
  57.  
  58.                                   Page 100
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                      Chapter 14 - Example Programs
  69.  
  70.  
  71.              Display the program on your monitor or print it out for
  72.         reference.   It  is  merely a loop watching for  a  keyboard
  73.         input or a change in the time.  If either happens, it reacts
  74.         accordingly.   In line 32,  the function "kbhit()" returns a
  75.         value  of 1 if a key has been hit but not yet read from  the
  76.         input buffer by the program.
  77.  
  78.              Look at the function named "get_time" for an example of
  79.         a  DOS call.   An interrupt 21(hex) is called after  setting
  80.         the  AH  register to 2C(hex) =  44(decimal).   The  time  is
  81.         returned in the CH,  CL, and DH registers.  Refer to the DOS
  82.         call  definitions in your copy of DOS.   If the  definitions
  83.         are  not included there,  Peter Nortons  book,  "Programmers
  84.         Guide  to  the  IBM PC" is recommended as a  good  reference
  85.         manual   for   these  calls  and  many   other   programming
  86.         techniques.   Note  that  Turbo  C  has  a  function   named
  87.         "gettime"  that does the same thing.  You should spend  some
  88.         time  studying the Turbo C Reference Guide to learn  of  the
  89.         availability of such functions.
  90.  
  91.              Another useful function is the "pos_cursor()"  function
  92.         that  positions the cursor anywhere on the monitor that  you
  93.         desire  by  using  a  DOS  interrupt.   In  this  case,  the
  94.         interrupt  used  is  10(hex) which is  the  general  monitor
  95.         interrupt.  This particular service is number 2 of about  10
  96.         different  monitor  services available.   This  function  is
  97.         included here as another example to you.
  98.  
  99.              The  next  function,  service  number  6  of  interrupt
  100.         10(hex)  is the window scroll service.   It should  be  self
  101.         explanatory.
  102.  
  103.              In this program, the cursor is positioned and some data
  104.         is  output  to the monitor,  then the cursor is "hidden"  by
  105.         moving  it  to line 26 which is not  displayed.   After  you
  106.         compile and run the program, you will notice that the cursor
  107.         is  not  visible on the monitor.   This is possible  in  any
  108.         program,  but  be  sure  to put the cursor  in  view  before
  109.         returning  to  DOS  because  DOS does not  like  to  have  a
  110.         "hidden" cursor and may do some strange things.
  111.  
  112.              Some time spent studying this program will be  valuable
  113.         to  you as it will reveal how the keyboard data is input  to
  114.         the  computer.   Especially of importance is how the special
  115.         keys such as function keys, arrows, etc. are handled.   Also
  116.         note that this program uses full prototype checking and is a
  117.         good  example  of  how to use it.  Since it  also  uses  the
  118.         "modern"  method  of  function definitions,  it  is  a  good
  119.         example of that also.
  120.  
  121.  
  122.  
  123.  
  124.                                   Page 101
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                      Chapter 14 - Example Programs
  135.  
  136.  
  137.                   WHATNEXT.C - The Batch File Interrogator
  138.  
  139.              This  is  an  example of how to read the  data  on  the
  140.         command line following the function call.  Notice that there
  141.         are  two variables listed within the  parentheses  following
  142.         the main() call.   The first variable is a count of words in
  143.         the entire command line including the command itself and the
  144.         second  variable  is  a  pointer to  an  array  of  pointers
  145.         defining the actual words on the command line.
  146.  
  147.              First the question on the command line, made up of some
  148.         number of words, is displayed on the monitor and the program
  149.         waits for the operator to hit a key.   If the key hit is one
  150.         of  those  in the last "word" of the group of words  on  the
  151.         command  line,  the number of the character within the group
  152.         is  returned to the program where it can be tested with  the
  153.         "errorlevel" command in the batch file.   You could use this
  154.         technique  to  create a variable AUTOEXEC.BAT  file  or  any
  155.         other  batch  file  can  use this for  a  many  way  branch.
  156.         Compile  and  run this file with TEST.BAT for an example  of
  157.         how  it  works in practice.   You may  find  this  technique
  158.         useful  in  one  of  your batch files and  you  will  almost
  159.         certainly  need  to  read in  the  command  line  parameters
  160.         someday.
  161.  
  162.              An  interesting alternative would be for you to write a
  163.         program  named "WOULD.C" that would return a 1 if a  "Y"  or
  164.         "y"  were typed and a zero if any other key were hit.   Then
  165.         your batch file could have a line such as;
  166.  
  167.         WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  168.  
  169.              Dos would use "WOULD" as the program name,  ignore  the
  170.         rest  of  the  statement  except for displaying  it  on  the
  171.         screen.   You  would  then respond to the  question  on  the
  172.         monitor  with a single keyhit.   Your batch file would  then
  173.         respond   to  the  1  or  0  returned  and  either  run  the
  174.         alternative  part  of  the batch file or  the  primary  part
  175.         whatever each part was.
  176.  
  177.         WOULD YOU LIKE PRIMARY (Y/N)
  178.         IF ERRORLEVEL 1 GOTO PRIMARY
  179.         (secondary commands)
  180.         GOTO DONE
  181.         :PRIMARY
  182.         (primary commands)
  183.         :DONE
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.                                   Page 102
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                      Chapter 14 - Example Programs
  201.  
  202.  
  203.                         LIST.C - The Program Lister
  204.  
  205.              This program is actually composed of two files,  LIST.C
  206.         and  LISTF.C  that must be separately  compiled  and  linked
  207.         together  with your linker.   There is nothing new here  and
  208.         you  should  have  no  trouble compiling  and  linking  this
  209.         program  by  reading the documentation  supplied  with  your
  210.         Turbo C compiler.
  211.  
  212.              A  LIST.PRJ file is included on the SOURCE disk of  the
  213.         tutorial  as  an aid to you in compiling  and  linking  this
  214.         program.   Read pages 62 and 63 of the Turbo C  Users  Guide
  215.         for instructions on how to do it.
  216.  
  217.              The  only  thing  that is new in this  program  is  the
  218.         inclusion   of  three  "extern"  variables  in  the  LISTF.C
  219.         listing.   The only purpose for this is to tie these  global
  220.         variables  to  the main program and tell the  compiler  that
  221.         these  are not new variables.   The compiler will  therefore
  222.         not  generate any new storage space for them but simply  use
  223.         their names during the compile process.   At link time,  the
  224.         linker  will  get  their actual storage locations  from  the
  225.         LIST.OBJ  file and use those locations for the variables  in
  226.         the  LISTF part of the memory map also.   The  variables  of
  227.         those  names in both files are therefore the same  identical
  228.         variables and can be used just as any other global variables
  229.         could be used if both parts of the program were in one file.
  230.  
  231.              There is no reason why the variables couldn't have been
  232.         defined  in the LISTF.C part of the program and declared  as
  233.         "extern"  in the LIST.C part.   Some of the variables  could
  234.         have  been  defined  in one and some in the  other.   It  is
  235.         merely a matter of personal taste.   Carried to an  extreme,
  236.         all of the variables could have been defined in a third file
  237.         and  named "extern" in both of these files.   The third file
  238.         would then be compiled and included in the linking process.
  239.  
  240.              It would be to your advantage to compile, link, and run
  241.         this  program to prepare you for the next program  which  is
  242.         composed of 6 separate files which must all work together.
  243.  
  244.                         VC.C - The Visual Calculator
  245.  
  246.              This  program  finally ties nearly everything  together
  247.         because  it uses nearly every concept covered in the  entire
  248.         tutorial.   It  is so big that I will not even try to  cover
  249.         the finer points of its operation.   Only a few of the  more
  250.         important points will be discussed.
  251.  
  252.              The  first  thing  you  should do  is  go  through  the
  253.         tutorial  for  VC included in the file  VC.DOC.   There  are
  254.  
  255.  
  256.                                   Page 103
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                      Chapter 14 - Example Programs
  267.  
  268.  
  269.         several  dozen  steps  for you to execute,  with  each  step
  270.         illustrating some aspect of the Visual Calculator.  You will
  271.         get  a  good feel for what it is capable of doing  and  make
  272.         your study of the source code very profitable.  In addition,
  273.         you  will  probably  find  many  ways  to  use  the   Visual
  274.         Calculator  to  solve problems involving calculations  where
  275.         the  simplicity  of  the problem at hand  does  not  warrant
  276.         writing a program.
  277.  
  278.              Notice that the structure definitions,  used in all  of
  279.         the  separate parts of the program,  are defined in the file
  280.         STRUCT.DEF.   During  program development,  when  it  became
  281.         necessary  to change one of the structures slightly,  it was
  282.         not  necessary to change it in all of the  files,  only  one
  283.         file  required modification which was then "included" in the
  284.         source files.   Notice that the transcript data is stored in
  285.         a doubly linked list with the data itself being stored in  a
  286.         separate  dynamically allocated char string.   This line  is
  287.         pointed to by the pointer "lineloc".
  288.  
  289.              For  ease  of development,  the similar functions  were
  290.         grouped together and compiled separately.   Thus, all of the
  291.         functions  involving the monitor were included in  the  file
  292.         named  VIDEO.C,  and all of the functions involving the data
  293.         storage were grouped into the FILE.C  collection.   Dividing
  294.         your  program  in  a  way similar to  this  should  simplify
  295.         debugging and future modifications.
  296.  
  297.              Of special interest is the "monitor()" function.   This
  298.         function  examines  the  video mode through  use  of  a  DOS
  299.         command  and  if it is a 7,  it assumes it is  a  monochrome
  300.         monitor,  otherwise it assumes a color monitor.   The colors
  301.         of  the various fields are established at this time and used
  302.         throughout  the  program.   Most  of  the  data  is  written
  303.         directly  to the video memory,  but some is written  through
  304.         the standard BIOS routines.
  305.  
  306.              The file DEFIN.H is a catalogue of the functions to aid
  307.         in finding the functions.  This file was generated as one of
  308.         the  first  files  and was maintained and  updated  for  use
  309.         during  the  entire  design and coding  lifetime.   It  also
  310.         contains all of the prototype definitions for the  functions
  311.         in  all  of  the source files, and is  "included"  in  every
  312.         source file to do prototype checking.
  313.  
  314.              The  file  VC.PRJ  is included as an  aid  for  you  to
  315.         compile  and  link this program.  The Visual  Calculator  is
  316.         supplied  to you as VC.EXE already compiled and  linked  for
  317.         you.  Feel free, after understanding this code, to modify it
  318.         in any way you desire for your own use.
  319.  
  320.  
  321.  
  322.                                   Page 104
  323.